const Service = require('egg').Service;

class CompanyService extends Service {

	async info(company_code) {

		const { app } = this;
		const companyinfo = await app.redis.get('system').hget('dlq_company_code', company_code);

		if(companyinfo == null){
			const result = await this.app.mysql.get('weshop').query(
				'select id as wxid, appid, appsecret, mch_id, md5_key, sys_area, pay_wxid, cash_wxid, toredis_state, filename, filecontent, msg_template ' +
				'from qm_wx_account where FIND_IN_SET(?, sys_area) ', [company_code]
			);

			if(result.length == 0) {
				return undefined;
			}

			await app.redis.get('system').hset('dlq_company_code', company_code, JSON.stringify(result[0]));

			return result[0];
		} else {
			return JSON.parse(companyinfo);
		}

	}


	async infoByWXid(wxid) {

		const { app } = this;
		const companyinfo = await app.redis.get('system').hget('dlq_wx', wxid);

		return JSON.parse(companyinfo);

	}


	async accesstoken(wxid){

		//this.app.logger.info('accesstoken-- wxid ' + wxid);

		const result = await this.app.redis.get('system').hget('dlq_token', wxid);

		//this.app.logger.info(result);

		if(result == ''){
			return undefined;
		}else{
			return JSON.parse(result);
		}
		
	}


	//数据库机构存redis
	async toRedis () {

  		const { app } = this;

		const result = await this.app.mysql.get('weshop').query(
			'select id as wxid, appid, appsecret, mch_id, md5_key, sys_area, pay_wxid, cash_wxid, toredis_state, filename, filecontent, msg_template ' +
			'from qm_wx_account ', []
		);

		result.forEach(async (item) => {
			if(item.sys_area != null && item.sys_area != ''){
				item.sys_area.split(',').forEach(async (company_code) => {
					await app.redis.get('system').hset('dlq_company_code', company_code, JSON.stringify(item));
				});
			}

			await app.redis.get('system').hset('dlq_wx', item.wxid, JSON.stringify(item));

			//await app.redis.hset('dlq_token', item.wxid, null);
		})

		return result;
	}


	async domainFile (name) {

		const { app } = this;

		const result = await this.app.mysql.get('weshop').query(
			'select filecontent ' +
			'from qm_wx_account where filename=?', [name]
		);

		return result[0];
	}

  	async account() {

		const { app, ctx } = this;

		//ctx.logger.info(ctx._user.company_code);

		const result = await this.app.mysql.get('weshop').query(
			'select id as wxid, appid, appsecret, mch_id, md5_key, sys_area, pay_wxid, cash_wxid, toredis_state, filename, filecontent, msg_template ' +
			'from qm_wx_account where FIND_IN_SET(?, sys_area) ', [ctx._user.company_code]
		);

		return result[0];

	}


	async updateAccount (para) {

		const { app, ctx } = this;

		const { wxid, appid, appsecret, filename, filecontent, msg_template } = para;

		const res = await this.app.mysql.get('weshop').query(
			'update qm_wx_account set '+
			'appid=?, '+
			'appsecret=?, '+
			'filename=?, '+
			'filecontent=?, '+
			'msg_template=?' + 
			'where '+
			'id=? ', [appid, appsecret, filename, filecontent, msg_template, wxid]
		);

		return res.affectedRows === 1;
	}

	
}

module.exports = CompanyService;

